/**
 *
 */
package com.cuitrip.component.autoload;

import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

import java.lang.ref.SoftReference;


/**
 * 滚动后，自动加载数据
 */
public class AutoLoadScrollListener implements OnScrollListener {

    boolean mIsEnable = true;
    int mTotalCount = 0;
    int mLastItem = 0;
    SoftReference<Runnable> mLoadRunnableSoftReference;
    SoftReference<View> mLoadingSoftReference;
    private int leftCount = 2;
    private PositionCallBack positionCallBack;

    public AutoLoadScrollListener(Runnable loadRunnable) {
        mLoadRunnableSoftReference = new SoftReference<Runnable>(loadRunnable);
        mLoadingSoftReference = null;
    }

    public AutoLoadScrollListener(Runnable loadRunnable, PositionCallBack positionCallBack) {
        this(loadRunnable);
        setPositionCallBack(positionCallBack);
    }

    public AutoLoadScrollListener(Runnable loadRunnable, View loadingView) {
        mLoadRunnableSoftReference = new SoftReference<Runnable>(loadRunnable);
//        View view = loadingView.findViewById(android.R.id.progress);
        mLoadingSoftReference = new SoftReference<>(loadingView);
    }

    public void setLoadingView(View loadingView){
        View view = loadingView.findViewById(android.R.id.progress);
        mLoadingSoftReference = new SoftReference<>(view);
    }

    public void onLoadChanged(boolean isLoading) {
        if (mLoadingSoftReference != null && mLoadingSoftReference.get()!=null) {
            if (mLoadingSoftReference.get() != null) {
                mLoadingSoftReference.get().setVisibility(isLoading ? View.VISIBLE : View.GONE);
            }
        }
    }

    /**
     * set to true when first time data loaded , set to false when no more data
     * to load
     *
     * @param e
     */
    public void setEnable(boolean e) {
        mIsEnable = e;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        mLastItem = firstVisibleItem + visibleItemCount - 1;

    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == SCROLL_STATE_IDLE && mLastItem >= view.getCount() - leftCount && mLoadRunnableSoftReference != null && mLoadRunnableSoftReference.get() != null) {
            if (mIsEnable && mLoadRunnableSoftReference.get() != null) {
                mLoadRunnableSoftReference.get().run();
            }
        }
        if (view.getChildCount() > 0) {
            if (view.getFirstVisiblePosition() == 0 &&
                    view.getChildAt(0).getTop() == view.getPaddingTop()) {
                if (positionCallBack != null) {
                    positionCallBack.onScrollTop();
                }
            }else{
                if (positionCallBack != null) {
                    positionCallBack.onScrollLeftTop();
                }

            }
        }
    }

    public PositionCallBack getPositionCallBack() {
        return positionCallBack;
    }

    public void setPositionCallBack(PositionCallBack positionCallBack) {
        this.positionCallBack = positionCallBack;
    }

    public interface PositionCallBack {

        public void onScrollTop();

        public void onScrollLeftTop();

    }
}